package com.grep.base.util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
/**
 * 
 * @author yuyue z_yuguangyue@boco.com.cn
 * 
 * version <br>
 * 
 *
 * Description: .适合当前读取网页使用的文件封装类
 * fullPath 中，根据当前的要求，文件名称取最终页URL的ID值+ ".html"
 *
 * CreateTime: Jan 13, 2012  1:45:38 PM
 *
 * Change History:
 *
 *        Date             CR Number              Name              Description of change
 *
 *
 */
public class FileUtil { 
	
	/**
	 * 使用固定编码读取文件内容
	 * @param filename
	 * @param code
	 * @return
	 */
	public static String readFileByCharset(String filename,Charset charset ){
		if(charset ==null){
			return readFile(filename) ;
		}
		String content = "" ;
		File f = new File(filename);   
		InputStreamReader read = null ;
		BufferedReader reader = null ;
		try {
			read = new InputStreamReader (new FileInputStream(f),charset);
			reader=new BufferedReader(read);   
			String line;   
			while ((line = reader.readLine()) != null) {   
				content += line +"\n";
			}  
		} catch (Exception e) {
			e.printStackTrace() ;
		} finally{
			try {
				if(read != null){
					read.close();
				}
				if(reader != null){
					reader.close() ;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return content ;
	}
	/**
	 * 读取文件内容
	 * @param filename
	 * @param code
	 * @return
	 */
	public static String readFile(String filename){
		String content = "" ;
		FileReader fr = null ;
		BufferedReader reader = null;
		try {
			File file = new File(filename) ;
			fr = new FileReader(file) ;
			reader = new BufferedReader(fr);
			String tempString = null ;
			while((tempString = reader.readLine()) !=null){
				content = content + tempString ;
			}
		} catch ( Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(fr != null){
					fr.close() ;
				}
				if(reader != null){
					reader.close() ;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return content ;
	}
	//直接用FileWriter写文件，效率最差。
	public static boolean writeFileUseStrFW(String fullPath, String filecontent){
		FileWriter fw = null;
		try {
			fw = new FileWriter(fullPath);  
			fw.write(filecontent,0,filecontent.length());  
			fw.flush();
			return true ;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally{
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	} 
	//OutputStreamWriter 流的方式写，目前与BufferedOutputStream效果差不多，因为我们没有buffer
	public static void writeFileUseStrOW(String fullPath, String filecontent){
		OutputStreamWriter osw = null;
		try {
		  osw = new OutputStreamWriter(new FileOutputStream(fullPath));  
			osw.write(filecontent,0,filecontent.length());  
			osw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  finally{
			try {
				osw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	} 
	//写超大文件时，效果应该比较明显
	public static void writeFileUseStrBW(String fullPath, String filecontent){
		BufferedOutputStream osw = null;
		try {
			osw = new BufferedOutputStream(new FileOutputStream(fullPath)); 
			osw.write(filecontent.getBytes(),0,filecontent.length());  
			osw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  finally{
			try {
				osw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	} 
	public static boolean writeFileByCharset(String filename ,String charset,String content){
		OutputStream os = null ;
		OutputStreamWriter osw  = null ;
		BufferedWriter bw  =  null ;
		PrintWriter out = null ;
		try {
			os = new FileOutputStream(filename);
			osw =new OutputStreamWriter(os,charset);
			bw = new BufferedWriter(osw) ;
			out = new PrintWriter(bw);
			out.write(content) ;
			os.flush() ;
			osw.flush() ;
			bw.flush() ;
			out.flush() ;
			return true ;
		} catch (IOException e) {
			e.printStackTrace();
			return false ;
		} finally{
			try {
				if(os != null){
					os.close() ;
				}
				if(osw != null){
					osw.close() ;
				}
				if(bw != null){
					bw.close() ;
				}
				if(out != null){
					out.close() ;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
	}
	
	public static void main(String[] args) {
//		FileUtil fu = new FileUtil();
//		String s = "I am very GOOD";
//		long begintime = System.currentTimeMillis();
//		fu.writeFileUseStrBW("d:/fwtext2.txt", s);
//		System.out.println("use time" +( System.currentTimeMillis() - begintime));
		FileUtil fu = new FileUtil();
		System.out.println(fu.readFile("itunes.apple.com.cn.genre//dongzuo//56C7F5901CB5C08F9F132685342974421.html") );

	}

}
